import 'dart:convert';

import 'package:http/http.dart';
import 'package:spring_dict/Entities/DictDirection.dart';
import 'package:spring_dict/Entities/DictItem.dart';
import 'package:spring_dict/Entities/SearchMode.dart';
import 'package:spring_dict/Entities/SearchRequest.dart';
import 'package:spring_dict/Entities/SearchResponse.dart';
import 'package:spring_dict/Store/State.dart';
import 'package:spring_dict/UI/Pages/SearchPage/SearchPage.dart';
import 'package:spring_dict/global.dart';
import 'package:http/http.dart' as http;

const String baseUrl = "http://118.31.76.35:8000/api/v1";
const String baseUrlv0_2 = "http://118.31.76.35:8000/api/v1.1";

const String ON_DIRECTION_UPDATED = "dict.directions.updated";
const String ON_DIRECTION_SELECTED = "dict.directions.selectedOneItem";
const String ON_SEARCH_MODE_SELECTED = "dict.searchmode.selectedOneItem";
const String ON_APP_BOOT = "app.boot";
const String ON_SEARCH_REQUEST = "dict.search.request";
const String ON_SEARCH_SUCCESS = "dict.search.success";
const String ON_NETWORK_ERROR = "dict.search.network-error";

void initEvents() {

  globalBus.subscribe<bool>(ON_APP_BOOT, (bus, data) async {
    var response = await http.get("$baseUrl/");

    if (response.statusCode == 200) {
      directions = (jsonDecode(response.body) as List<dynamic>).map((item) => DictDirection.fromJson(item)).toList();

      selectedDirection = DictDirection(
          sourceRtl: false,
          destinationRtl: false,
          source: "ئاپتوماتىك",
          code: "auto-auto",
          destination: "ئاپتوماتىك"
      );

      directions.add(selectedDirection);

      globalBus.publish(ON_DIRECTION_UPDATED, directions);
      globalBus.publish(ON_SEARCH_MODE_SELECTED, SearchMode.startswith);

    } else
      globalBus.publish(ON_NETWORK_ERROR, response);
  });

  globalBus.subscribe<DictDirection>(ON_DIRECTION_SELECTED, (bus, direction){
    selectedDirection = direction;
  });

  globalBus.subscribe<SearchRequest>(ON_SEARCH_REQUEST, (bus, request)async {
    var mode = "equals";
    switch (request.searchMode){
      case SearchMode.startswith: mode = "startswith";break;
      case SearchMode.contains: mode = "contains"; break;
      case SearchMode.endswith: mode = "endswith"; break;
    }
    var response = await http.get("$baseUrlv0_2/Search?keyWord=${request.keyWord}&searchMode=$mode&from=${selectedDirection.code}&count=50&pageIndex=0");
    if (response.statusCode == 200){
      var result = SearchResponse.fromJson(json.decode(response.body));
      searchResponse = result;

      globalBus.publish<SearchResponse>(ON_SEARCH_SUCCESS, searchResponse);
      searchRequest = request;
      if (selectedDirection.code != result.code)
        globalBus.publish(ON_DIRECTION_SELECTED, directions.firstWhere((item) => item.code == result.code));
    }
    else
      {
        globalBus.publish(ON_NETWORK_ERROR, response);
      }
  });

  globalBus.subscribe<Response>(ON_NETWORK_ERROR, (bus, response){
    log("Network error... StatusCode: ${response.statusCode}");
  });

  globalBus.subscribe<SearchMode>(ON_SEARCH_MODE_SELECTED, (bus, mode){
    selectedSearchMode = mode;
  });
}
